package com.ddwl.goods.service;

import com.ddwl.common.exception.CommonException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.goods.dao.ItemParamDao;
import com.ddwl.goods.dao.model.ItemParam;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.ItemParamBo;
import com.ddwl.schema.bo.goods.ItemParamCreateBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.goods.ItemParamListVo;
import com.ddwl.schema.vo.goods.ItemParamVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.protobuf.MapEntry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemParamService {

    @Autowired
    private ItemParamDao itemParamDao;

    @Transactional(rollbackFor = Throwable.class)
    public List<String> create(ItemParamCreateBo bo) {
        ItemParam iParam = new ItemParam();
        iParam.setCategoryId(bo.getCategoryId());
            List<String> list = new ArrayList<>();
            List<ItemParam> itemParams=itemParamDao.select(iParam);
        if (itemParams.size()==0) {
            for (String param : bo.getParamNames()) {
                ItemParam itemParam = new ItemParam();
                itemParam.setCategoryId(bo.getCategoryId());
                itemParam.setParamName(param);
                itemParamDao.insert(itemParam);
                list.add(itemParam.getId());
            }
        }else {
            throw new CommonException("该分类已经设置参数！");
        }
        return list;
    }

    @Transactional(rollbackFor = Throwable.class)
    public void update(ItemParamCreateBo bo) {
        itemParamDao.deleteByCategoryId(bo.getCategoryId());
        for (String param : bo.getParamNames()) {
            ItemParam itemParam = new ItemParam();
            itemParam.setCategoryId(bo.getCategoryId());
            itemParam.setParamName(param);
            itemParamDao.insert(itemParam);
        }
    }

    public PageVo<ItemParamListVo> list(PageBo<ItemParam> bo) {
        PageHelper.startPage(bo.getPage(), bo.getPageSize(), MySqlUtil.orderBy(bo.getSort(), bo.isAsc()));
        List<ItemParamListVo> itemParamListVos = new ArrayList<>();
        //传了分类id按分类id查，没有传分类id则查全部的
        if (bo.getParam().getCategoryId() != null) {
        ItemParamListVo itemParamListVo = new ItemParamListVo();
            List<ItemParam> itemParams = itemParamDao.select(bo.getParam());
            List<String> paramNames = new ArrayList<>();
            for (ItemParam itemParam : itemParams) {
                paramNames.add(itemParam.getParamName());
            }
            itemParamListVo.setCategoryId(bo.getParam().getCategoryId());
            itemParamListVo.setParamNames(paramNames);
            itemParamListVos.add(itemParamListVo);
        } else {
            List<ItemParam> itemParams = itemParamDao.selectAll();
            Map<String, List<String>> map = new HashMap<>();
            //根据分类id分组
            for (ItemParam itemParam : itemParams) {
                if (map.containsKey(itemParam.getCategoryId())) {
                    map.get(itemParam.getCategoryId()).add(itemParam.getParamName());
                } else {
                    List<String> params = new ArrayList<>();
                    params.add(itemParam.getParamName());
                    map.put(itemParam.getCategoryId(), params);
                }
            }
            for (Map.Entry<String, List<String>> entry : map.entrySet()) {
                ItemParamListVo itemParamListVo=new ItemParamListVo();
                itemParamListVo.setCategoryId(entry.getKey());
                itemParamListVo.setParamNames(entry.getValue());
                itemParamListVos.add(itemParamListVo);
            }
        }
        PageInfo<ItemParamListVo> pageInfo = new PageInfo(itemParamListVos);
        return new PageVo<>(pageInfo.getTotal(), bo.getPage(), bo.getPageSize(), pageInfo.getList());
    }
}
